package com.wowangz.cms.systemmanage.translator.api;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.alibaba.fastjson.JSONObject;
import com.wowangz.cms.component.datagrid.pojo.DataGrid;
import com.wowangz.cms.component.datagrid.pojo.DataResult;
import com.wowangz.cms.component.datagrid.pojo.DataStyle;
import com.wowangz.cms.component.datagrid.pojo.PangingParam;
import com.wowangz.cms.component.datagrid.utils.DataTypeConstant;
import com.wowangz.cms.component.datagrid.utils.PangingTools;
import com.wowangz.cms.syscore.IssTransactionContainer;
import com.wowangz.cms.syscore.api.BaseController;
import com.wowangz.cms.syscore.api.JSONFactory;
import com.wowangz.cms.syscore.utils.ServletUtils;
import com.wowangz.cms.systemmanage.translator.pojo.EditTranslatorObj;
import com.wowangz.cms.systemmanage.translator.pojo.ListTranslatorsResponse;
import com.wowangz.cms.systemmanage.translator.pojo.TblTags;
import com.wowangz.cms.systemmanage.translator.pojo.TblTranslator;
import com.wowangz.cms.systemmanage.translator.pojo.TranslatorWithAbility;
import com.wowangz.cms.systemmanage.translator.service.CoreServerAPIService;
import com.wowangz.cms.systemmanage.translator.service.TranslatorService;
import com.wowangz.cms.systemmanage.translator.service.TranslatorServiceNew;

@Scope("prototype")
@Controller
public class TranslatorController extends BaseController {

	@Autowired
	private TranslatorService translatoService;

	@Autowired
	private CoreServerAPIService coreServerAPIService;

	/**
	 * 创建译员页面
	 * 
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "/systemmanage/translator/addTranslator.web")
	public String addTranslator(Model model) {
		List<TblTags> tagList = translatoService.getTags();
		model.addAttribute("tagList", tagList);
		return "/WEB-INF/jsp/systemmanage/translator/addTranslator";
	}

	/**
	 * 保存译员
	 * 
	 * @param request
	 * @param response
	 * @param model
	 * @throws IOException
	 */
	@RequestMapping(value = "/systemmanage/translator/saveTranslator.json", method = { RequestMethod.GET, RequestMethod.POST })
	public void createTranslator(HttpServletRequest request, HttpServletResponse response, Model model) throws IOException {
		TblTranslator translator = (TblTranslator) ServletUtils.getParametersToBaseEntity(request, TblTranslator.class);
		String ablityIds = request.getParameter("ablityIds");
		String translatorTags = request.getParameter("translatorTags");
		try{
			translatoService.register(translator, ablityIds, translatorTags);
		}catch(RuntimeException e){
			response.getWriter().write("{\"result\":\"" + e.getMessage() + "\"}");
			response.getWriter().flush();
			response.getWriter().close();
		}
		response.getWriter().write("{\"result\":\"1\"}");
		response.getWriter().flush();
		response.getWriter().close();
	}

	/**
	 * 译员编辑页面
	 * 
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "/systemmanage/translator/editTranslator.web")
	public String editTranslator(HttpServletRequest request, HttpServletResponse response, Model model) {
		String translatorId = request.getParameter("translatorId");
		model.addAttribute("translatorId", translatorId);
		List<TblTags> tagList = translatoService.getTags();
		model.addAttribute("tagList", tagList);
		return "/WEB-INF/jsp/systemmanage/translator/editTranslator";
	}

	/**
	 * 译员数据初始化
	 * 
	 * @param model
	 * @return
	 * @throws IOException
	 */
	@RequestMapping(value = "/systemmanage/translator/initTranslator.json")
	public void initTranslator(HttpServletRequest request, HttpServletResponse response) throws IOException {
		String translatorId = request.getParameter("translatorId");
		TranslatorWithAbility translatorWithProperty = translatoService.selectTranslatorWithProperty(translatorId);
		EditTranslatorObj obj = new EditTranslatorObj();
		obj.setTranslatorWithProperty(translatorWithProperty);
		response.getWriter().write(JSONObject.toJSONString(obj));
		response.getWriter().flush();
		response.getWriter().close();
	}

	/**
	 * 更新译员
	 * 
	 * @param request
	 * @param response
	 * @param model
	 * @throws IOException
	 */
	@RequestMapping(value = "/systemmanage/translator/updateTranslator.json", method = { RequestMethod.GET, RequestMethod.POST })
	public void updateTranslator(HttpServletRequest request, HttpServletResponse response, Model model) throws IOException {
		TblTranslator translator = (TblTranslator) ServletUtils.getParametersToBaseEntity(request, TblTranslator.class);
		String ablityIds = request.getParameter("ablityIds");
		String translatorTags = request.getParameter("translatorTags");
		try {
			translatoService.update(translator, ablityIds, translatorTags);
		} catch (RuntimeException e) {
			response.getWriter().write("{\"result\":\"" + e.getMessage() + "\"}");
			response.getWriter().flush();
			response.getWriter().close();
		}
		response.getWriter().write("{\"result\":\"1\"}");
		response.getWriter().flush();
		response.getWriter().close();
	}

	@RequestMapping(value = "/systemmanage/translator/managerTranslator.web")
	public String managerTranslator(HttpServletRequest request, HttpServletResponse response, Model model) {
		return "/WEB-INF/jsp/systemmanage/translator/managerTranslator";
	}

	@RequestMapping(value = "/systemmanage/translator/listTranslator.json")
	public void listTranslator(HttpServletRequest request, HttpServletResponse response, Model model) {
		PangingParam pangingParam = null;
		DataResult dataResult = new DataResult();
		DataGrid dataGrid = null;

		try {
			pangingParam = (PangingParam) ServletUtils.getParametersToBaseEntity(request, PangingParam.class);
			HashMap<String, Object> map = ServletUtils.getParametersToHashMap(request);
			dataGrid = new DataGrid();
			dataGrid.setPangingParam(pangingParam);

			map.put("startrow", dataGrid.getStartrow());
			map.put("endrow", dataGrid.getEndrow());
			map.put("rows", dataGrid.getRows());
			map.put("sort", dataGrid.getSort());
			map.put("order", dataGrid.getOrder());

			List<ListTranslatorsResponse> resultList = translatoService.listTranslators(map);
			long resultCount = translatoService.translatorsCountBymybatis(map);

			DataStyle dataStyle = new DataStyle();
			dataStyle.setPageIndex(pangingParam.getPage());
			dataStyle.setDataCount(resultCount);
			dataStyle.setDataList(resultList);

			dataStyle.addDepict("translatorId", DataTypeConstant.STRING);
			dataStyle.addDepict("translatorType", DataTypeConstant.STRING);
			dataStyle.addDepict("trueName", DataTypeConstant.STRING);
			dataStyle.addDepict("nickName", DataTypeConstant.STRING);
			dataStyle.addDepict("regType", DataTypeConstant.STRING);
			dataStyle.addDepict("thirdPartyId", DataTypeConstant.STRING);
			dataStyle.addDepict("phone", DataTypeConstant.STRING);
			dataStyle.addDepict("email", DataTypeConstant.STRING);
			dataStyle.addDepict("langAbilitys", DataTypeConstant.STRING);
			dataStyle.addDepict("dataStatus", DataTypeConstant.STRING);
			dataStyle.addDepict("montherTongue", DataTypeConstant.STRING);
			dataStyle.addDepict("onlineStatus", DataTypeConstant.STRING);
			dataStyle.addDepict("registerTime", DataTypeConstant.STRING);
			dataStyle.addDepict("tags", DataTypeConstant.STRING);

			dataResult = PangingTools.buildResultPagerInfo(dataStyle);

			if (pangingParam.getReportExportType() == 0) {
				response.getWriter().write(JSONFactory.toJSONString(dataResult, userContext));
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * 译员禁用
	 * 
	 * @param request
	 * @param response
	 * @param model
	 * @throws IOException
	 */
	@RequestMapping(value = "/systemmanage/translator/block.json", method = { RequestMethod.GET, RequestMethod.POST })
	public void block(HttpServletRequest request, HttpServletResponse response, Model model) throws IOException {
		String translatorId = request.getParameter("translatorId");
		translatoService.block(translatorId);
		response.getWriter().write("{\"result\":\"1\"}");
		response.getWriter().flush();
		response.getWriter().close();
	}

	/**
	 * 译员解禁
	 * 
	 * @param request
	 * @param response
	 * @param model
	 * @throws IOException
	 */
	@RequestMapping(value = "/systemmanage/translator/unblock.json", method = { RequestMethod.GET, RequestMethod.POST })
	public void unblock(HttpServletRequest request, HttpServletResponse response, Model model) throws IOException {
		String translatorId = request.getParameter("translatorId");
		translatoService.unblock(translatorId);
		response.getWriter().write("{\"result\":\"1\"}");
		response.getWriter().flush();
		response.getWriter().close();
	}

	@RequestMapping(value = "/systemmanage/translator/managerTranslatornew.web")
	public String managerTranslatornew(HttpServletRequest request, HttpServletResponse response, Model model) {
		return "/WEB-INF/jsp/systemmanage/translator/managerTranslatornew";
	}

	@RequestMapping(value = "/systemmanage/translator/listTranslatornew.json")
	public void listTranslatornew(HttpServletRequest request, HttpServletResponse response, Model model) {
		PangingParam pangingParam = null;
		DataResult dataResult = new DataResult();
		DataGrid dataGrid = null;

		try {
			pangingParam = (PangingParam) ServletUtils.getParametersToBaseEntity(request, PangingParam.class);
			HashMap<String, Object> map = ServletUtils.getParametersToHashMap(request);
			dataGrid = new DataGrid();
			dataGrid.setPangingParam(pangingParam);

			map.put("startrow", dataGrid.getStartrow());
			map.put("endrow", dataGrid.getEndrow());
			map.put("rows", dataGrid.getRows());
			map.put("sort", dataGrid.getSort());
			map.put("order", dataGrid.getOrder());

			TranslatorServiceNew translatorServiceNew = (TranslatorServiceNew) IssTransactionContainer.getInstance(TranslatorServiceNew.class, false);

			List<ListTranslatorsResponse> resultList = translatorServiceNew.listTranslators(map);
			long resultCount = translatorServiceNew.translatorsCount(map);

			DataStyle dataStyle = new DataStyle();
			dataStyle.setPageIndex(pangingParam.getPage());
			dataStyle.setDataCount(resultCount);
			dataStyle.setDataList(resultList);

			dataStyle.addDepict("translatorId", DataTypeConstant.STRING);
			dataStyle.addDepict("translatorType", DataTypeConstant.STRING);
			dataStyle.addDepict("trueName", DataTypeConstant.STRING);
			dataStyle.addDepict("nickName", DataTypeConstant.STRING);
			dataStyle.addDepict("regType", DataTypeConstant.STRING);
			dataStyle.addDepict("thirdPartyId", DataTypeConstant.STRING);
			dataStyle.addDepict("phone", DataTypeConstant.STRING);
			dataStyle.addDepict("email", DataTypeConstant.STRING);
			dataStyle.addDepict("langAbilitys", DataTypeConstant.STRING);
			dataStyle.addDepict("dataStatus", DataTypeConstant.STRING);
			dataStyle.addDepict("montherTongue", DataTypeConstant.STRING);
			dataStyle.addDepict("onlineStatus", DataTypeConstant.STRING);
			dataStyle.addDepict("registerTime", DataTypeConstant.STRING);
			dataStyle.addDepict("tags", DataTypeConstant.STRING);

			dataResult = PangingTools.buildResultPagerInfo(dataStyle);

			if (pangingParam.getReportExportType() == 0) {
				response.getWriter().write(JSONFactory.toJSONString(dataResult, userContext));
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * 刷新
	 * 
	 * @param request
	 * @param response
	 * @param model
	 * @throws IOException
	 */
	@RequestMapping(value = "/systemmanage/translator/refresh.json", method = { RequestMethod.GET, RequestMethod.POST })
	public void refresh(HttpServletRequest request, HttpServletResponse response, Model model) throws IOException {
		boolean flag = coreServerAPIService.pushTranslatorToCoreServer();
		if (flag) {
			response.getWriter().write("{\"result\":\"1\"}");
		} else {
			response.getWriter().write("{\"result\":\"0\"}");
		}
		response.getWriter().flush();
		response.getWriter().close();
	}

}
